using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace Middlerware.Sample
{
    public class Program
    {
        public static void Main(string[] args)
        {
            TestClass test = new TestClass();
            var list= test.FourSum(new int[] { 1, -2, -5, -4, -3, 3, 3, 5 }, -11);

            CreateHostBuilder(args).Build().Run();
        }



        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                });
    }


    public class TestClass
    {
        public int Flag { get; set; }
        public string Change(string s)
        {
            s = "abc";
            return s;
        }
        public string Change2(ref string s)
        {
            s = "abc";
            return s;
        }

        public IList<IList<int>> FourSum(int[] nums, int target)
        {
            IList<IList<int>> list = new List<IList<int>>();
            if (nums.Length < 3) return list;

            nums = SimpleSort(nums);

            for (int m = 0; m < nums.Length - 3; m++)
            {
                int x = nums[m];
                if (m > 0 && nums[m] == nums[m - 1])
                {
                    continue;
                }
                int tempTarget = target - nums[m];
    
                for (int i = m + 1; i < nums.Length - 2; i++)
                {
                    int a = nums[i];

                    if (i > m + 1 && nums[i] == nums[i - 1])
                    {
                        continue;
                    }
                    int k = nums.Length - 1;
                    for (int j = i + 1; j < nums.Length - 1; j++)
                    {
                        int b = nums[j];
                        if (j > i + 1 && nums[j] == nums[j - 1])
                        {
                            continue;
                        }

                        while (j < k && a + b + nums[k] > tempTarget)
                        {
                            k--;
                        }

                        if (j == k)
                        {
                            break;
                        }
                        if (a + b + nums[k] == tempTarget)
                        {
                            list.Add(new List<int> { x, a, b, nums[k] });
                        }
                    }
                }
            }
            return list;
        }

        public int[] SimpleSort(int[] list)
        {
            for (int i = 0; i < list.Length - 1; i++)
            {
                int m = i;
                for (int j = i + 1; j < list.Length; j++)
                {
                    if (list[m] > list[j])
                    {
                        m = j;
                    }
                }
                int temp = list[i];
                list[i] = list[m];
                list[m] = temp;
            }
            return list;

        }
    }
}
